#pragma once

#include <optional>
#include <QString>
#include "../nlohmann/json.hpp"

using json = nlohmann::json;

class AlarmRule {
public:
    std::optional<int> ruleId;
    std::optional<int> deviceId;
    std::optional<std::string> description;
    std::optional<std::string> condition;
    std::optional<std::string> action;

    json toJson() const {
        json j;
        j["ruleId"] = ruleId ? json(*ruleId) : json(nullptr);
        j["deviceId"] = deviceId ? json(*deviceId) : json(nullptr);
        j["description"] = description ? json(*description) : json(nullptr);
        j["condition"] = condition ? json(*condition) : json(nullptr);
        j["action"] = action ? json(*action) : json(nullptr);
        return j;
    }

    static AlarmRule fromJson(const json &j) {
        AlarmRule r;
        if (j.contains("ruleId") && !j["ruleId"].is_null()) r.ruleId = j["ruleId"].get<int>();
        if (j.contains("deviceId") && !j["deviceId"].is_null()) r.deviceId = j["deviceId"].get<int>();
        if (j.contains("description") && !j["description"].is_null())
            r.description = j["description"].get<std::string>();
        if (j.contains("condition") && !j["condition"].is_null()) r.condition = j["condition"].get<std::string>();
        if (j.contains("action") && !j["action"].is_null()) r.action = j["action"].get<std::string>();
        return r;
    }
};

